package cn.tom.service;

import cn.tom.entity.Usr;
import cn.tom.dao.UserMapper;
import org.apache.catalina.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userDao;
    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public Usr findByAccount(String account) {
        return this.userDao.findByAccount(account);
    }

    @Override
    public Usr findByEmail(String str) {
        return this.userDao.findByEmail(str);
    }

    @Override
    public Usr findById(int id) {
        return this.userDao.findById(id);
    }

    @Override
    public void add(Usr u) {
        String pass = u.getPassword();
        if (pass != null) {
            u.setPassword(this.passwordEncoder.encode(pass));
        }
        this.userDao.add(u);
    }

    @Override
    public List<Usr> findAll(Map<String, Object> map) {
        return this.userDao.findAll(map);
    }

    @Override
    public int getCount() {
        return this.userDao.getCount();
    }

    @Override
    public int update(Usr u) {
        if (u.getPassword() != null && u.getPassword().length() > 0) {
            u.setPassword(this.passwordEncoder.encode(u.getPassword()));
        }
        return this.userDao.update(u);
    }

    @Override
    public int updatePwd(int uid, String newPwd) {
        if (newPwd != null || newPwd.length() > 1) {
            return this.userDao.updatePwd(uid, this.passwordEncoder.encode(newPwd));
        }
        return 0;
    }

    @Override
    public int updateImg(int uid, String uuid) {
        return this.userDao.updateImg(uid, uuid);
    }

    @Override
    public List<Usr> findPageUsr(Integer num, Integer num2) {
        return this.userDao.findPageUsr(num,num2);
    }

    @Override
    public int freeze(int closed, Integer uid) {
        return this.userDao.freeze(closed,uid);
    }

    @Override
    public int reset(int uid) {
        return this.userDao.reset(uid);
    }
}
